export default {
	namespaced: true,
	state: {
		// 购物车内的物品		
		shopCar: {},
		//总价格
		sumPrice: 0
	},
	getters: {
		checkSize(state) { // 选择的种类数量
			return Object.keys(state.shopCar).length
		}
	},
	mutations: {
		addShopCar(state, val) {
			var shopCar = JSON.parse(JSON.stringify(state.shopCar))
			
			if (!shopCar[val.id]) {
				shopCar[val.id] = {
					"obj": val,
					"buy": 1
				}
			} else {
				shopCar[val.id].buy++
			}
			
			state.shopCar = shopCar
			state.sumPrice = parseFloat((state.sumPrice + shopCar[val.id].obj.prePrice).toFixed(2))
			
			// console.log(shopCar)
		},
		dec(state, id) {
			var shopCar = JSON.parse(JSON.stringify(state.shopCar))
			
			if (shopCar[id].buy == 1) {
				state.sumPrice = parseFloat((state.sumPrice - shopCar[id].obj.prePrice).toFixed(2))
				delete shopCar[id]
				state.shopCar = shopCar
			} else if (shopCar[id].buy > 1) {
				shopCar[id].buy--
				state.shopCar = shopCar
				state.sumPrice = parseFloat((state.sumPrice - shopCar[id].obj.prePrice).toFixed(2))
			}
		},
		add(state, id) {
			var shopCar = JSON.parse(JSON.stringify(state.shopCar))
			shopCar[id].buy++
			state.shopCar = shopCar
			state.sumPrice = parseFloat((state.sumPrice + shopCar[id].obj.prePrice).toFixed(2))
		},
		clearCar(state) { // 清空当前购物车
			state.shopCar = {}
			state.checkSize = 0
			state.sumPrice = 0
		}
	},
	actions: { 
	}
}